package com.jprocms.module.cms.controller.admin.comment;

import com.jpro.framework.apilog.core.annotation.ApiAccessLog;
import com.jpro.framework.common.pojo.CommonResult;
import com.jpro.framework.common.pojo.PageParam;
import com.jpro.framework.common.pojo.PageResult;
import com.jpro.framework.common.util.collection.CollectionUtils;
import com.jpro.framework.common.util.object.BeanUtils;
import com.jpro.framework.excel.core.util.ExcelUtils;
import com.jpro.framework.security.core.util.SecurityFrameworkUtils;
import com.jprocms.module.cms.controller.admin.comment.vo.CommentPageReqVO;
import com.jprocms.module.cms.controller.admin.comment.vo.CommentRespVO;
import com.jprocms.module.cms.controller.admin.comment.vo.CommentSaveReqVO;
import com.jprocms.module.cms.dal.dataobject.comment.CommentDO;
import com.jprocms.module.cms.dal.dataobject.content.ContentDO;
import com.jprocms.module.cms.enums.EnumCommentStatus;
import com.jprocms.module.cms.service.comment.CommentService;
import com.jprocms.module.cms.service.content.ContentCountService;
import com.jprocms.module.cms.service.content.ContentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

import static com.jpro.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.jpro.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 内容评论")
@RestController
@RequestMapping("/cms/comment")
@Validated
public class CommentController {

    @Resource
    private CommentService commentService;
    @Resource
    private ContentService contentService;
    @Resource
    private ContentCountService contentCountService;

    @PostMapping("/create")
    @Operation(summary = "创建内容评论")
    @PreAuthorize("@ss.hasPermission('cms:comment:create')")
    public CommonResult<Long> createComment(@Valid @RequestBody CommentSaveReqVO createReqVO) {
        return success(commentService.createComment(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新内容评论")
    @PreAuthorize("@ss.hasPermission('cms:comment:update')")
    public CommonResult<Boolean> updateComment(@Valid @RequestBody CommentSaveReqVO updateReqVO) {
        commentService.updateComment(updateReqVO);
        return success(true);
    }

    @PostMapping("/reply")
    @Operation(summary = "回复内容评论")
    @PreAuthorize("@ss.hasPermission('cms:comment:update')")
    public CommonResult<Boolean> replyComment(@Valid @RequestBody CommentSaveReqVO updateReqVO) {
        updateReqVO.setReplyUserId(SecurityFrameworkUtils.getLoginUserId());
        updateReqVO.setReplyTime(LocalDateTime.now());
        commentService.updateComment(updateReqVO);
        return success(true);
    }

    @PostMapping("/pass")
    @Operation(summary = "通过内容评论")
    @PreAuthorize("@ss.hasPermission('cms:comment:update')")
    public CommonResult<Boolean> passComment(@RequestParam("id") Long id) {
        CommentSaveReqVO commentSaveReqVO = new CommentSaveReqVO();
        commentSaveReqVO.setStatus(EnumCommentStatus.CHECK_PASS.getStatus());
        commentSaveReqVO.setId(id);
        commentService.updateComment(commentSaveReqVO);
        commentService.updateContentCommentCount(id, true);
        return success(true);
    }

    @PostMapping("/reject")
    @Operation(summary = "不通过内容评论")
    @PreAuthorize("@ss.hasPermission('cms:comment:update')")
    public CommonResult<Boolean> rejectComment(@RequestParam("id") Long id) {
        CommentSaveReqVO commentSaveReqVO = new CommentSaveReqVO();
        commentSaveReqVO.setStatus(EnumCommentStatus.CHECK_REJECT.getStatus());
        commentSaveReqVO.setId(id);
        commentService.updateComment(commentSaveReqVO);
        commentService.updateContentCommentCount(id, false);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除内容评论")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cms:comment:delete')")
    public CommonResult<Boolean> deleteComment(@RequestParam("id") Long id) {
        commentService.deleteComment(id);
        return success(true);
    }

    @DeleteMapping("/deleteByIds")
    @Operation(summary = "删除内容评论")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cms:comment:delete')")
    public CommonResult<Boolean> deleteByIds(@RequestBody List<Long> ids) {
        commentService.deleteByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得内容评论")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cms:comment:query')")
    public CommonResult<CommentRespVO> getComment(@RequestParam("id") Long id) {
        CommentDO comment = commentService.getComment(id);
        return success(BeanUtils.toBean(comment, CommentRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得内容评论分页")
    @PreAuthorize("@ss.hasPermission('cms:comment:query')")
    public CommonResult<PageResult<CommentRespVO>> getCommentPage(@Valid CommentPageReqVO pageReqVO) {
        PageResult<CommentDO> pageResult = commentService.getCommentPage(pageReqVO);
        final PageResult<CommentRespVO> commentRespVOPageResult = BeanUtils.toBean(pageResult, CommentRespVO.class);
        final List<CommentRespVO> list = commentRespVOPageResult.getList();
        final List<Long> contentIds = CollectionUtils.convertList(list, CommentRespVO::getContentId);
        final List<ContentDO> contentList = contentService.getContentList(contentIds);
        final Map<Long, ContentDO> contentIdContentMap = CollectionUtils.convertMap(contentList, ContentDO::getId, c -> c);
        for (CommentRespVO commentRespVO : list) {
            commentRespVO.setContentTitle(contentIdContentMap.get(commentRespVO.getContentId()).getTitle());
            commentRespVO.setContentUrl(contentIdContentMap.get(commentRespVO.getContentId()).getUrl());
        }
        return success(commentRespVOPageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出内容评论 Excel")
    @PreAuthorize("@ss.hasPermission('cms:comment:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCommentExcel(@Valid CommentPageReqVO pageReqVO,
                                   HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CommentDO> list = commentService.getCommentPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "内容评论.xls", "数据", CommentRespVO.class,
                BeanUtils.toBean(list, CommentRespVO.class));
    }


}